package com.jinzecloud.fiction.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jinzecloud.fiction.constant.UserConstant;
import com.jinzecloud.fiction.vo.TbUserVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinzecloud.fiction.utils.PageUtils;
import com.jinzecloud.fiction.utils.Query;

import com.jinzecloud.fiction.mapper.TbUserMapper;
import com.jinzecloud.fiction.bean.TbUser;
import com.jinzecloud.fiction.service.TbUserService;


@Service("tbUserService")
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements TbUserService {

    @Autowired
    TbUserMapper tbUserMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<TbUser> tbUserQueryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            tbUserQueryWrapper.eq("us_id",key);
        }
        if (!StringUtils.isEmpty(key)){
            tbUserQueryWrapper.or().like("us_user_name",key);
        }
        if (!StringUtils.isEmpty(key)){
            tbUserQueryWrapper.or().like("us_signature",key);
        }
        IPage<TbUser> page = this.page(
                new Query<TbUser>().getPage(params), tbUserQueryWrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        List<TbUser> records = page.getRecords();
        List<TbUserVo> list = records.stream().map(TbUser -> {
            TbUserVo tbUserVo = new TbUserVo();
            BeanUtils.copyProperties(TbUser, tbUserVo);
            String msg = UserConstant.User.getMsg(tbUserVo.getUsStatus());
            tbUserVo.setUsStatus(msg);
            return tbUserVo;
        }).collect(Collectors.toList());
        pageUtils.setList(list);
        return pageUtils;
    }

    @Override
    public void upStatus(TbUser tbUser) {
        // TODO 待处理状态的编写 自动解封状态编写
        UpdateWrapper<TbUser> tbUserUpdateWrapper = new UpdateWrapper<>();
        tbUserUpdateWrapper.eq("us_id",tbUser.getUsId()).set("us_status",tbUser.getUsStatus());
        tbUserMapper.update(null,tbUserUpdateWrapper);
    }

    @Override
    public void updateTbUserById(TbUser tbUser) {
        String password = SecureUtil.md5(tbUser.getUsPassword());
        String upDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(new Date());
        UpdateWrapper<TbUser> updateWrapper = new UpdateWrapper<TbUser>()
                .eq("us_id", tbUser.getUsId())
                .set("us_update_time", upDateTime)
                .set("us_password",password)
                .set("us_profile_photo",tbUser.getUsProfilePhoto());
        tbUserMapper.update(null,updateWrapper);
    }

    @Override
    public void saveTbUser(TbUser tbUser) {
        tbUser.setUsPassword(SecureUtil.md5(tbUser.getUsPassword()));
        String creationDatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(new Date());
        tbUser.setUsCreationTime(creationDatetime);
        tbUser.setUsUpdateTime(creationDatetime);
        tbUser.setUsStatus("0");
        tbUserMapper.insert(tbUser);
    }

    @Override
    public List<TbUser> getUserList() {
        List<TbUser> list = tbUserMapper.selectList(null);
        List userList = list.stream().map(tbUser -> {
            Map<String,Object> map = new HashMap<>();
            map.put("value",tbUser.getUsId().toString());
            map.put("label",tbUser.getUsUserName());
            return map;
        }).collect(Collectors.toList());

        return userList;
    }

}